home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE2 / PD / VINCE / !ViNCe / c / sockets < prev    next >
Text File  |  2002-06-30  |  4KB  |  184 lines

  1. /*
  2.  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
  3.  *
  4.  *  This is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This software is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this software; if not, write to the Free Software
  16.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  17.  *  USA.
  18.  */
  19.  
  20. /*
  21.  * sockets.c - functions to deal with sockets.
  22.  */
  23.  
  24.  
  25. #include <time.h>
  26.  
  27. #include "oslib/socket.h"
  28.  
  29. #include "vncviewer.h"
  30. #include "ip.h"
  31.  
  32.  
  33. Bool errorMessageOnReadFailure = True;
  34.  
  35. #define BUF_SIZE 8192
  36. static char buf[BUF_SIZE];
  37. static char *bufoutptr = buf;
  38. int buffered = 0;
  39.  
  40. static int totalread = 0;
  41. /*
  42.  * ReadFromRFBServer is called whenever we want to read some data from the RFB
  43.  * server.  It is non-trivial for two reasons:
  44.  *
  45.  * 1. For efficiency it performs some intelligent buffering, avoiding invoking
  46.  *    the read() system call too often.  For small chunks of data, it simply
  47.  *    copies the data out of an internal buffer.  For large amounts of data it
  48.  *    reads directly into the buffer provided by the caller.
  49.  *
  50.  * 2. Whenever read() would block, it invokes the Xt event dispatching
  51.  *    mechanism to process X events.  In fact, this is the only place these
  52.  *    events are processed, as there is no XtAppMainLoop in the program.
  53.  */
  54.  
  55.  
  56. Bool ReadFromRFBServer(char *out, unsigned int n) {
  57.  
  58.   if (n <= buffered) {
  59.     memcpy(out, bufoutptr, n);
  60.     bufoutptr += n;
  61.     buffered -= n;
  62.     return True;
  63.   }
  64.  
  65.   if (buffered > 0)    memcpy(out, bufoutptr, buffered);
  66.  
  67.   out += buffered;
  68.   n -= buffered;
  69.  
  70.   bufoutptr = buf;
  71.   buffered = 0;
  72.  
  73.   if (n <= BUF_SIZE) {
  74.  
  75.     while (buffered < n) {
  76.       int i, ready, to_read;
  77.  
  78.       ready = ip_ready(rfbsock);
  79.       if (ready > BUF_SIZE - buffered)
  80.         to_read = BUF_SIZE - buffered;
  81.       else
  82.         to_read = ready;
  83.       i = ip_read(rfbsock, buf + buffered, to_read);
  84.       if (i < 0)
  85.         return False;
  86.       totalread += i;
  87.       buffered += i;
  88.       poll();
  89.     }
  90.  
  91.     memcpy(out, bufoutptr, n);
  92.     bufoutptr += n;
  93.     buffered -= n;
  94.  
  95.   } else {
  96.  
  97.     while (n > 0) {
  98.       int i, ready, to_read;
  99.  
  100.       ready = ip_ready(rfbsock);
  101.       if (ready > BUF_SIZE - buffered)
  102.         to_read = BUF_SIZE - buffered;
  103.       else
  104.         to_read = ready;
  105.       i = ip_read(rfbsock, out, to_read);
  106.       if (i < 0)    return False;
  107.       totalread += i;
  108.       out += i;
  109.       n -= i;
  110.       poll();
  111.     }
  112.  
  113.   }
  114.   return True;
  115. }
  116.  
  117.  
  118. /*
  119.  * Write an exact number of bytes, and don't return until you've sent them.
  120.  */
  121.  
  122. Bool WriteExact(int sock, void *buf, int n) {
  123.  
  124.   int i = 0;
  125.  
  126.   for(;;) {
  127.     int j;
  128.  
  129.     j = ip_write(sock, (char *)buf + i, (n - i));
  130.     if (j <= 0)
  131.       return False;
  132.     i += j;
  133.     if (i == n)
  134.       return True;
  135.     poll();
  136.   }
  137. }
  138.  
  139.  
  140. /*
  141.  * ConnectToTcpAddr connects to the given TCP port.
  142.  */
  143.  
  144. int
  145. ConnectToTcpAddr(unsigned int host, int port)
  146. {
  147.   int sock;
  148.  
  149.   sock = ip_create(0);
  150.   if (sock < 0) {
  151.     fprintf(stderr, "Failed to create socket\n");
  152.     return -1;
  153.   }
  154.  
  155.   if (ip_connect(sock, host, port) <= 0) {
  156.     fprintf(stderr, "Failed to connect on port %d\n", port);
  157.     ip_close(sock);
  158.     return -1;
  159.   }
  160.  
  161.   ip_nonblocking(sock);
  162.  
  163.   return sock;
  164. }
  165.  
  166.  
  167.  
  168. /*
  169.  * SetNonBlocking sets a socket into non-blocking mode.
  170.  */
  171.  
  172. Bool SetNonBlocking(int sock) {
  173.   ip_nonblocking(sock);
  174.   return True;
  175. }
  176.  
  177. /*
  178.  * CloseSocket
  179.  */
  180. void CloseSocket(int sock) {
  181.   fprintf(stderr, "BYTES READ %d  (%d)\n", totalread, clock());
  182.   ip_close(sock);
  183. }
  184.